package com.example.front.controller;

import com.example.front.service.ICartService;
import com.example.front.service.UserService;
import com.example.gok_mall.constUtil.ConstUtil;
import com.example.gok_mall.entity.User;
import com.example.gok_mall.reponseData.ResponseCode;
import com.example.gok_mall.reponseData.ResultData;
import com.example.front.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/cart")
@ResponseBody
public class CartController {

    @Autowired
    private ICartService iCartService;

    @Autowired
    private UserService userService;


    //查询购物车中商品
    @GetMapping("list.do")
    public ResultData<CartVo> list(HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iCartService.list(user.getId());
        } else {
            return ResultData.fail("查询失败");
        }
    }

    //添加商品到购物车
    @PostMapping("add.do")
    public ResultData<CartVo> add(HttpSession session, Integer productId, Integer count) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iCartService.add(user.getId(), productId, count);
        } else {
            return ResultData.fail("添加失败");
        }
    }

    //更新商品到购物车
    @PostMapping("update.do")
    public ResultData<CartVo> update(HttpSession session, Integer count, Integer productId) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iCartService.update(user.getId(), productId, count);
        } else {
            return ResultData.fail("更新失败");
        }
    }

    //删除购物车中的商品
    @DeleteMapping("delete_product.do")
    public ResultData<CartVo> deleteProduct(HttpSession session, String productIds) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iCartService.deleteProduct(user.getId(), productIds);
        } else {
            return ResultData.fail("删除失败");
        }
    }

    //全选
    @GetMapping("select_all.do")
    public ResultData<CartVo> selectAll(HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iCartService.selectOrUnselectAll(user.getId(), ConstUtil.Cart.CHECKED, null);
        } else {
            return ResultData.fail("选择失败");
        }
    }

    //全反选
    @GetMapping("un_select_all.do")
    public ResultData<CartVo> unSelectAll(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iCartService.selectOrUnselectAll(user.getId(), ConstUtil.Cart.UN_CHECKED, null);
        } else {
            return ResultData.fail("反选失败");
        }
    }

    //单独选
    @GetMapping("select.do")
    public ResultData<CartVo> Select(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iCartService.selectOrUnselectAll(user.getId(), ConstUtil.Cart.CHECKED, productId);
        } else {
            return ResultData.fail("选择失败");
        }
    }

    //单独反选
    @GetMapping("un_select.do")
    public ResultData<CartVo> unSelect(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iCartService.selectOrUnselectAll(user.getId(), ConstUtil.Cart.UN_CHECKED, productId);
        } else {
            return ResultData.fail("反选失败");
        }
    }

    //查询当前用户购物车的数量，如果一个产品数量有十个，我们显示是个
    @GetMapping("get_cart_product_count.do")
    public ResultData<Integer> getCartProductCount(HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iCartService.getCartProductCount(user.getId());
        } else {
            return ResultData.fail("查询失败");
        }
    }
}

